#include "DistinguishSurface.h"
#include "ui_DistinguishSurface.h"
#include <Kernel/GunInfo.h>
#include <Kernel/Screenshot.h>

DistinguishSurface::DistinguishSurface(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::DistinguishSurface)
{
    ui->setupUi(this);
    ui->preview->scale(2,2);
    update_gun_list();
}

DistinguishSurface::~DistinguishSurface()
{
    delete ui;
}

void DistinguishSurface::on_save_to_list_left_clicked()
{
    std::string name = ui->gun_name->text().toStdString();
    unsigned int hash = ui->gun_hash->text().toUInt();
    bool is_shake = ui->is_shake->isChecked();
    if(name.empty() || hash == 0u){
        INFO("info is invalid");
        return;
    }
    GunInfo::add_left(name,hash,is_shake);
    ui->left_gun_list_view->update_data(GunInfo::get_left_gun_map());
}

void DistinguishSurface::on_save_to_list_right_clicked()
{
    std::string name = ui->gun_name->text().toStdString();
    unsigned int hash = ui->gun_hash->text().toUInt();
    bool is_shake = ui->is_shake->isChecked();
    if(name.empty() || hash == 0u){
        INFO("info is invalid");
        return;
    }
    GunInfo::add_right(name,hash,is_shake);
    ui->right_gun_list_view->update_data(GunInfo::get_right_gun_map());
}

void DistinguishSurface::on_reset_clicked()
{
    ui->gun_name->clear();
    ui->gun_hash->clear();
    ui->is_shake->setChecked(false);
}

void DistinguishSurface::on_create_left_gun_clicked()
{
    auto frame_desc = Screenshot::cut_frame(GunInfo::left_x,GunInfo::left_y,GunInfo::left_w,GunInfo::left_h);
    int hash{0};
    for(auto i = 0u;i<frame_desc.size();i++){
        hash += frame_desc.data[i];
    }
    ui->gun_hash->setText(QString::number(hash));
    ui->preview->graphics_scence->addPixmap(QPixmap::fromImage(QImage(frame_desc.data,frame_desc.width,frame_desc.height,QImage::Format::Format_RGB32)));
}

void DistinguishSurface::on_create_right_gun_clicked()
{
    auto frame_desc = Screenshot::cut_frame(GunInfo::right_x,GunInfo::right_y,GunInfo::right_w,GunInfo::right_h);
    int hash{0};
    for(auto i = 0u;i<frame_desc.size();i++){
        hash += frame_desc.data[i];
    }
    ui->gun_hash->setText(QString::number(hash));
    ui->preview->graphics_scence->addPixmap(QPixmap::fromImage(QImage(frame_desc.data,frame_desc.width,frame_desc.height,QImage::Format::Format_RGB32)));
}

void DistinguishSurface::update_gun_list()
{
    ui->left_gun_list_view->update_data(GunInfo::get_left_gun_map());
    ui->right_gun_list_view->update_data(GunInfo::get_right_gun_map());
}
